﻿using MaiPlanServer.IRepository;
using MaiPlanServer.IServices.IReservoirServices;
using MaiPlanServer.IServices.IWarehouseServives;
using MaiPlanServer.Model;
using MaiPlanServer.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;

namespace MaiPlanServer.API.Controllers.ReservoirManage
{
    /// <summary>
    /// 库区
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class ReservoirController : ControllerBase
    {
        public ReservoirController(IReservoirManageServices RMS)
        {
            this.RMS = RMS;
        }

        public IReservoirManageServices RMS { get; }
        /// <summary>
        /// 获取库区设置表数据
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="LOCATIONName"></param>
        /// <param name="LOCATIONcode"></param>
        /// <param name="warehouse_id">搜索;当前库区隶属仓库</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int PageIndex, int PageSize, string LOCATIONName, string LOCATIONcode, string warehouse_id)
        {
            try
            {
                var list = await RMS.GetArmazemAsync(PageIndex, PageSize, LOCATIONName, LOCATIONcode, warehouse_id);
                var count = await RMS.GetCountAsync(LOCATIONName, warehouse_id);

                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "请求失败ex"));
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] WM_STORAGE_LOCATION model)
        {
            try
            {
                model.LOCATION_ID = Guid.NewGuid();
                model.CREATE_TIME = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                var result = await RMS.AddAsync(model);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "新增成功"));
                }
                return Ok(BaseState.CreateInstance(200, "新增失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "新增失败，服务器异常，请联系管理员"));
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> Put([FromForm] WM_STORAGE_LOCATION model)
        {
            try
            {
                var result = await RMS.UpdateAsync(model);

                return Ok(BaseState.CreateInstance(200, "修改成功"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "修改失败，服务器异常，请联系管理员"));
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Del(string Id)
        {
            try
            {
                var result = await RMS.DellAsync(Id);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }
        /// <summary>
        /// 删除多条
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpDelete("DelAll")]
        public async Task<IActionResult> DelAll(string Id)
        {
            try
            {
                string[] ids = Id.Split(',');
                var result = await RMS.DellAllAsync(ids);
                if (result)
                {
                    return Ok(BaseState.CreateInstance(200, "删除成功"));
                }
                return Ok(BaseState.CreateInstance(-1, "删除失败"));
            }
            catch (Exception ex)
            {
                return Ok(BaseState.CreateInstance(BaseState.RequestError, "删除失败，服务器异常，请联系管理员"));
            }
        }
        [HttpGet("GETwarhou")]
        public async Task<IActionResult> Getwarehouse()
        {
            try
            {
                var list = await RMS.Getwarehouse();
                return Ok(BaseState.CreateInstance(200, "请求成功",list));
            }
            catch (Exception ex)
            {

                throw;
                return Ok(BaseState.CreateInstance(-1, "请求失败，服务器异常"));
            }
        }

        [HttpGet("GetLocation")]
        public async Task<IActionResult> GetLocation(int PageIndex, int PageSize, string id)
        {
            try
            {
                var list = await RMS.GetLocation(PageIndex,  PageSize, id);
                var count = await RMS.GetLocationcount(id);
                return Ok(BaseState.CreateInstance(200, "请求成功", list, count));
            }
            catch (Exception ex)
            {

                throw;
            }

            return Ok(BaseState.CreateInstance(-1, "请求失败"));
        }


    }
}
